package com.example.mysqltry.service.impl;

import com.example.mysqltry.api.vo.NewBeeMallIndexConfigGoodsVO;
import com.example.mysqltry.dao.MallGoodsInfoMapper;
import com.example.mysqltry.dao.MallIndexConfigMapper;
import com.example.mysqltry.entity.MallGoodsInfo;
import com.example.mysqltry.entity.MallIndexConfig;
import com.example.mysqltry.service.NewBeeMallIndexConfigService;
import com.example.mysqltry.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NewBeeMallIndexConfigServiceImpl  implements NewBeeMallIndexConfigService {
    @Autowired
   private MallIndexConfigMapper mallIndexConfigMapper;
    @Autowired
    private MallGoodsInfoMapper mallGoodsInfoMapper;

    private List<NewBeeMallIndexConfigGoodsVO> newBeeMallIndexConfigGoodsVOS;
    @Override
   public List<NewBeeMallIndexConfigGoodsVO> getConfigGoodsesForIndex(int configType, int number)
    {
        System.out.println(configType+"configType"+number+"number");
        List<MallIndexConfig> mallIndexConfigs=mallIndexConfigMapper.findIndexConfigByTypeAndNum(configType,number);

        if(!CollectionUtils.isEmpty(mallIndexConfigs))
        {
            List<Long> goodIds=
                    mallIndexConfigs.stream().map(MallIndexConfig::getGoodsId).collect(Collectors.toList());
            List<MallGoodsInfo> mallGoodsInfos=mallGoodsInfoMapper.selectByPrimaryKeys(goodIds);
            System.out.println(mallGoodsInfos+"mallGoodsInfos");
            newBeeMallIndexConfigGoodsVOS= BeanUtil.copyList(mallGoodsInfos,NewBeeMallIndexConfigGoodsVO.class);
            for (NewBeeMallIndexConfigGoodsVO newBeeMallIndexConfigGoodsVO : newBeeMallIndexConfigGoodsVOS) {
                String goodsName = newBeeMallIndexConfigGoodsVO.getGoodsName();
                // 字符串过长导致文字超出的问题
                if (goodsName.length() > 30) {
                    goodsName = goodsName.substring(0, 30) + "...";
                    newBeeMallIndexConfigGoodsVO.setGoodsName(goodsName);
                }
            }

        }
        return  newBeeMallIndexConfigGoodsVOS;


    }
}
